<?php

namespace App\Console\Commands;

use Illuminate\Support\Arr;
use DB;
use Schema;
use Illuminate\Support\Str;

trait CommandTrait
{

    /**
     * @param $path
     * @return array
     * @author: lisa
     * @datetime: 2021/8/3 11:19
     */
    public function getNamespaceAndClassName($path)
    {
        $className = basename($path);
        $namespace = str_replace($className, '', $path);
        $namespace = rtrim($namespace, '/');
        $namespace = str_replace('/', '\\', $namespace);
        $namespace = Str::studly($namespace);
        $className = str_replace('.php', '', $className);
        return [$namespace, $className];
    }

    /**
     * 获取路径
     * @param $fileName
     * @return string
     * @author: lisa
     * @datetime: 2021/8/3 11:13
     */
    public function getPath($fileName)
    {
        return $this->basePath . '/' . $fileName;
    }

    /**
     * 获取文件名称
     * @param $fileName
     * @return string
     * @author: lisa
     * @datetime: 2021/8/3 11:14
     */
    public function getFileName($fileName)
    {
        return Str::studly($fileName);
    }

    /**
     * 判断表是否存在
     * @param $table
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 10:35
     */
    public function isTableExists($table)
    {
        return Schema::hasTable($table);
    }

    /**
     * 获取表结构
     * @param $table
     * @return mixed
     * @author: lisa
     * @datetime: 2021/8/3 10:54
     */
    public function getTableStructure($table)
    {
        $structure = DB::select('SHOW FULL COLUMNS FROM `' . $table . '`');
//        $structure = DB::select("DESC {$table}");
        return json_decode(json_encode($structure), true);
    }

    /**
     * @param $table
     * @return string
     * @author: lisa
     * @datetime: 2021/8/4 11:45
     */
    public function getTableComment($table)
    {
        $list    = Schema::getConnection()->getDoctrineSchemaManager()->listTables();
        $comment = '';
        foreach ($list as $k => $v) {
            if ($table == $v->getName()) {
                $comment = $v->getComment();
                break;
            }
        }
        return $comment;
    }

    /**
     * 是否不能为空
     * @param $value
     * @return int
     * @author: lisa
     * @datetime: 2021/8/3 13:38
     */
    public function isNotNull($value)
    {
        return $value == 'NO' ? 1 : 0;
    }

    /**
     * 是否为主键
     * @param $value
     * @return int
     * @author: lisa
     * @datetime: 2021/8/3 10:51
     */
    public function isPrimaryKey($value)
    {
        return $value == 'PRI' ? 1 : 0;
    }

    /**
     * 是否为数字
     * @param $value
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 11:02
     */
    public function isNumber($value)
    {
        return Str::startsWith($value, [
            'bigint',
            'int',
            'integer',
            'tinyint',
            'smallint',
            'mediumint',
            'decimal',
            'float',
            'double',
            'numeric',
        ]);
    }

    /**
     * 是否为整型
     * @param $value
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 11:02
     */
    public function isInteger($value)
    {
        return Str::startsWith($value, [
            'bigint',
            'int',
            'integer',
            'tinyint',
            'smallint',
            'mediumint',
        ]);
    }
    public function isTinyint($value)
    {
        return Str::startsWith($value, [
            'tinyint',
        ]);
    }

    /**
     * 是否为字符串
     * @param $value
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 11:06
     */
    public function isString($value)
    {
        return Str::startsWith($value, [
            'enum',
            'char',
            'varchar',
            'text',
            'tinytext',
            'mediumtext',
            'longtext',
        ]);
    }

    /**
     * 是否为文本
     * @param $value
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 11:06
     */
    public function isText($value)
    {
        return Str::startsWith($value, [
            'text',
            'tinytext',
            'mediumtext',
            'longtext',
        ]);
    }

    /**
     * 是否为时间格式
     * @param $value
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 11:10
     */
    public function isTime($value)
    {
        return Str::startsWith($value, [
            'time',
        ]);
    }

    /**
     * @param $value
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 12:45
     */
    public function isDate($value)
    {
        return Str::startsWith($value, [
            'date',
        ]);
    }

    /**
     * @param $value
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 12:45
     */
    public function isDateTime($value)
    {
        return Str::startsWith($value, [
            'datetime',
            'timestamp',
        ]);
    }

    /**
     * 是否写入
     * @param $path
     * @return bool
     * @author: lisa
     * @datetime: 2021/8/3 11:30
     */
    public function isWrite($path)
    {
        return !(file_exists($path) && !$this->confirm('文件 [ ' . $path . ' ] 已经存在是否覆盖？'));
    }

    /**
     * 是否为手机号
     * @param $field
     * @return int
     * @author: lisa
     * @datetime: 2021/8/3 14:25
     */
    public function isPhone($field)
    {
        return $field == 'phone' || $field == 'mobile' ? 1 : 0;
    }

    /**
     * 是否为电话
     * @param $field
     * @return int
     * @author: lisa
     * @datetime: 2021/8/3 14:26
     */
    public function isMobile($field)
    {
        return $field == 'contact_tel' || $field == 'tel_phone' || $field == 'tele_phone' ? 1 : 0;
    }

    /**
     * @param $field
     * @return bool
     * @author: lisa
     * @datetime: 2021/9/2 15:30
     */
    public function isAmount($field)
    {
        if (
            stripos($field, 'price') !== false ||
            stripos($field, 'amount') !== false ||
            stripos($field, 'money') !== false ||
            stripos($field, 'balance') !== false
        ) return true;
        else return false;
    }

    /**
     * 生成数组字符串
     * @param $array
     * @return string
     * @author: lisa
     * @datetime: 2021/8/3 14:04
     */
    public function createPreArrayString($array)
    {
        $str = "[\n";
        foreach ($array as $k => $v) {
            if (is_numeric($k)) {
                $str .= "\t\t\t'{$v}',\n";
            } else {
                $str .= "\t\t\t'{$k}' => '{$v}',\n";
            }
        }

        return $str . "\t\t]";
    }

    /**
     * @param $array
     * @return string
     * @author: lisa
     * @datetime: 2021/8/4 9:35
     */
    public function createPreTableColsString($array)
    {
        $str = "[\n";
        foreach ($array as $k => $v) {
            $str .= "\t\t\t{$v},\n";
        }

        return $str . "\t\t]";
    }

    /**
     * @param $array
     * @return string
     * @name:createDocArrayToString
     * @author: lisa
     * @datetime: 2022/2/22 13:30
     */
    public function createDocArrayToString($array)
    {
        return implode("\n ", $array);
    }

    /**
     * @param $array
     * @return string
     * @name:createPreFormatString
     * @author: lisa
     * @datetime: 2023-03-21 12:35
     */
    public function createPreFormatString($array)
    {
        $str = "[\n";
        foreach ($array as $k => $v) {
            $str .= "\t\t\t'{$v}' => ".'$resource'."->{$v},\n";
        }

        return $str . "\t\t]";
    }

    /**
     * @param $type
     * @return array|\ArrayAccess|mixed
     * @author: lisa
     * @datetime: 2021/8/3 13:48
     */
    public function getTypeMax($type)
    {
        preg_match('/\d+/', $type, $array);
        return Arr::get($array, 0);
    }

    /**
     * 渲染
     * @param $path
     * @param $params
     * @param $renderHtml
     * @author: lisa
     * @datetime: 2021/8/3 11:32
     */
    public function render($path, $params, $renderHtml)
    {
        $dirName = dirname($path);
        if (!file_exists($dirName)) @mkdir($dirName, 0755, true);
        if ($this->isWrite($path)) {
            $search  = array_map(function ($value) {
                return "{" . $value . "}";
            }, array_keys($params));
            $content = str_replace($search, array_values($params), $renderHtml);
            file_put_contents($path, $content);
            $this->info('文件 [ ' . $path . ' ] 生成成功');
        } else {
            $this->info('文件 [ ' . $path . ' ] 存在，不处理');
        }
    }
}
